package easypsync.app;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class PIMSyncActivity extends Activity {
	
//	private static final String TAG = PIMSyncActivity.class.getName();
	
	public static final String STARTSTOP_INTENT = "easypsync.STARTSTOP_INTENT";
	
	@Override
	protected void onStart() {
		super.onStart();
	}
	
    /** Called when the activity is first created. */
    @Override public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        stopService();
        startService();
        Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show();
        
    
        Button doSync = (Button)findViewById(R.id.doSync);
        // Register the onClick listener with the implementation above
        doSync.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				doSync();
		        Toast.makeText(getApplicationContext(), "Synchronization started", Toast.LENGTH_LONG).show();
			}});
        
        Button startService = (Button)findViewById(R.id.startService);
        // Register the onClick listener with the implementation above
        startService.setOnClickListener(new OnClickListener() {

			@Override public void onClick(View arg0) {
				stopService(); // stop first otherwise 2 times start crash the application.
				startService();
		        Toast.makeText(getApplicationContext(), "Service stopped and started", Toast.LENGTH_LONG).show();
			}});
        
        Button stopService = (Button)findViewById(R.id.stopService);
        // Register the onClick listener with the implementation above
        stopService.setOnClickListener(new OnClickListener() {

			@Override public void onClick(View arg0) {
				stopService();
		        Toast.makeText(getApplicationContext(), "Service stopped", Toast.LENGTH_LONG).show();
			}});
        
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			Intent settingsActivity = new Intent(getBaseContext(), Preferences.class);
			startActivity(settingsActivity);

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
	
    public void doSync() {
    	PIMSync ps = new PIMSync(this.getBaseContext());
    	ps.doSync();
    }
    
    public void startService() {
    	Intent i = new Intent(STARTSTOP_INTENT, null);
		getBaseContext().startService(i);
    }
    
    public void stopService() {
    	Intent i = new Intent(STARTSTOP_INTENT, null);
		getBaseContext().stopService(i);
    }
}